home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Development Tools & Languages / DTSCPlusLibrary / Sources / DTSCPlusLibrary.cp < prev    next >
Encoding:
Text File  |  1993-01-14  |  4.8 KB  |  182 lines  |  [TEXT/MPS ]

  1. /* _________________________________________________________________________________________________________ //
  2.   Copyright Â© 1992-93 Apple Computer, Inc. All rights reserved.
  3.   Macintosh Developer Technical Support.C++ Macintosh Toolbox Framework.
  4.   Date: Monday, June 22, 1992 11:44:15
  5.   Revision comments are at the end of this file.
  6.   ---
  7.   DTSCPlusLibrary.h contains all the global data and definitions needed when building
  8.   more DTS C++ libraries. Every class should have this file included.
  9.   DTSCPlusLibrary.cp contains functions that we could not inline inside the header (sigh).
  10.   _________________________________________________________________________________________________________ */
  11.  
  12. #ifndef _DTSCPLUSLIBRARY_
  13. #include "DTSCPlusLibrary.h"
  14. #endif
  15.  
  16. // _________________________________________________________________________________________________________ //
  17. // STRING FUNCTIONS
  18.  
  19. // return the len of a C string
  20. short clen(char *cptr)
  21. {
  22.     short    i;
  23.  
  24.     for (i = 0; cptr[i]; ++i)
  25.         ;
  26.     return(i);
  27. }
  28.  
  29.  
  30. // Convert a c-string to a pascal-string
  31.  
  32. void c2p(char *cptr)
  33. {
  34.     char    len;
  35.  
  36.     ::BlockMove(cptr, cptr + 1, len = clen(cptr));
  37.     *cptr = len;
  38. }
  39.  
  40.  
  41. // Convert a pascal-string to a c-string
  42. void p2c(StringPtr cptr)
  43. {
  44.     char    len;
  45.  
  46.     ::BlockMove(cptr + 1, cptr, len = *cptr);
  47.     cptr[len] = 0;
  48. }
  49.  
  50.  
  51. // Copy two Pascal strings
  52. void Pstrcpy(StringPtr d, StringPtr s)
  53. {
  54.     short    i;
  55.  
  56.     i = *s;
  57.     do {
  58.         d[i] = s[i];
  59.     } while (i--);
  60. }
  61.  
  62.  
  63. // Concatenate two Pascal strings.
  64. void ConcatPStrings(Str255 first,
  65.                            Str255 second)
  66. {
  67.     short charsToCopy;
  68.  
  69.     // Truncate if concatenated string would be longer than 255 chars
  70.     charsToCopy = Min(second[0], 255 - first[0]);
  71.     ::BlockMove(second + 1, first + first[0] + 1, (long)charsToCopy);
  72.     first[0] += charsToCopy;
  73. }
  74.  
  75. // Compare two Pascal Strings.
  76. Boolean CompareStr255(const Str255 *first, const Str255 *second)
  77. {
  78.     short state = ::RelString(*first, *second, true, true);            // call toolbox comparison routine
  79.     if(state == sortsEqual)
  80.         return true;
  81.     else
  82.         return false;
  83. }
  84.  
  85. //    vxdebugstr that also prints out by default __FILE__ and __LINE__ information
  86. void vxdebugstr(char* format,...) 
  87. {
  88.     char buff[255];
  89.     char final[255];
  90.  
  91.     va_list arglist;
  92.     va_start(arglist,format);
  93.     vsprintf(buff,format,arglist);
  94.     va_end(arglist);
  95.  
  96. #ifdef FILELINEINFO    
  97.     strcat(final,fileNameArray);
  98. #ifdef TESTLEVEL2
  99.     strcat(final, " ;");                            // MacsBug does not like embedded ;s
  100. #endif
  101.     strcat(final, lineNumberArray);
  102. #endif
  103.  
  104.     strcat(final, buff);
  105.     c2p(final);
  106.     
  107.     // Print out the message, based on the testing level
  108. #ifdef TESTLEVEL1
  109.     AlertUser((Str255)final);
  110. #endif
  111. #ifdef TESTLEVEL2
  112.     ::SysBreakStr((Str255)final);                        // for high level debuggers
  113. #endif
  114. #ifdef TESTLEVEL3
  115.     ::DebugStr((Str255)final);
  116. #endif
  117. }
  118.  
  119.  
  120. // ErrorWindow (no resources)
  121. // Here's an example of how to create a window with error information, without using Alerts or 
  122. // any other resource-bound constructs:
  123. // Pre-defined messages needed in the final box.
  124. const char* kLabelMessage = "Serious Problem:";
  125. const char* kInformMessage = "Click with the mouse or hit a key to terminate the application…";
  126.  
  127. void ErrorWindow(char* message)
  128. // Present a nice error window with a defined message.
  129. {
  130.     Rect windowRect, textRect, labelRect, informRect;
  131.     WindowPtr win;
  132.     
  133.     // Define a window size and create a window
  134.     ::SetRect(&windowRect, 90, 70, 400, 200);
  135.     win = ::NewWindow(NULL, &windowRect, "\p", true, dBoxProc, (WindowPtr)-1, false, 0L);
  136.         
  137.         if(win != NULL)
  138.         {
  139.             ::SetPort(win);            // switch to the right port
  140.             ::InitCursor();            // make sure the cursor is normal
  141.             // First draw the label:
  142.             ::SetRect(&labelRect, 20, 5, 300, 18);
  143.             ::TextFace(bold);
  144.             ::TextBox(kLabelMessage, strlen(kLabelMessage), &labelRect, teJustLeft);
  145.             // Then draw the message:
  146.             ::SetRect(&textRect, 20, 30, 300, 280);        // define a text rect
  147.             ::TextSize(9);
  148.             ::TextBox(message, strlen(message), &textRect, teJustLeft);
  149.     
  150.             // Draw the inform line at the bottom of the box
  151.             ::SetRect(&informRect,10, 115, 300, 280);
  152.             ::TextFace(normal);    
  153.             ::TextBox(kInformMessage, strlen(kInformMessage), &informRect, teJustLeft);
  154.                 
  155.             // now, spin around in WaitNextEvent until user hits a key click or hits the mouse
  156.             EventRecord anEvent;
  157.             while(!::WaitNextEvent(keyDownMask+mDownMask, &anEvent, 20L, NULL))
  158.                 ;
  159.             
  160.             // OK, we got the needed event, dispose the window.
  161.             ::DisposeWindow(win);
  162.         }    // Problems, we couldn't create a window, so all we could do is to beep for the end user.
  163.         else
  164.             ::SysBeep(1);
  165.         
  166.         ::ExitToShell();    // this is a serious problem, so we need to exit once and for all
  167. }
  168.  
  169. /* example of use: 
  170. ErrorWindow("Joe Montana can't play this Sunday: Error -9999");
  171. */
  172.  
  173. // _________________________________________________________________________________________________________ //
  174.  
  175. /*    Change History (most recent last):
  176.   No    Init.    Date        Comment
  177.   1        khs        9/10/92        New file
  178.   2        khs        11/26/92    Added ASSERT macros and testing levels
  179.   3        khs        1/14/93        Cleanup
  180. */
  181.  
  182.